﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmBoHoc : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        string _companyid = GlobalVariable.WSInfo.CompanyID;

        public frmBoHoc()
        {
            InitializeComponent();
        }

        private void frmBoHoc_Load(object sender, EventArgs e)
        {
            LoadControl();
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            ds.RejectChanges();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            UpdateData();
        }

        #region "Coding"
        private void LoadControl()
        {
            LoadData();
            LoadHocVien();
            LoadKhoaHoc();

            gridControl_BoHoc.DataSource = ds.Tables["TableBoHoc"];
            
            repositoryItemSearchLookUpEdit_InvoiceNumber.DataSource = ds.Tables["TableHocVien"];
            repositoryItemSearchLookUpEdit_InvoiceNumber.ValueMember = "InvoiceNumber";
            repositoryItemSearchLookUpEdit_InvoiceNumber.DisplayMember = "HoTen";

            repositoryItemSearchLookUpEdit_KhoaHoc.DataSource = ds.Tables["TableKhoaHoc"];
            repositoryItemSearchLookUpEdit_KhoaHoc.ValueMember = "MaKhoaHoc";
            repositoryItemSearchLookUpEdit_KhoaHoc.DisplayMember = "TenKhoaHoc";
        }

        private void LoadData()
        {
            try
            {
                if (ds.Tables["TableBoHoc"] != null)
                    ds.Tables["TableBoHoc"].Clear();
                string sql = @"select * from BoHoc";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableBoHoc");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadHocVien()
        {
            if (ds.Tables["TableHocVien"] != null)
                ds.Tables["TableHocVien"].Clear();
            string sql = @"select InvoiceNumber, (Holot + ' ' + Ten) as HoTen, MaLop
                            from Invoice inv inner join Enquiry enq on enq.EnquiryNumber = inv.EnquiryNumber
                            where inv.MaDonVi = '" + _companyid + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.Fill(ds, "TableHocVien");
        }

        private void LoadKhoaHoc()
        {
            if (ds.Tables["TableKhoaHoc"] != null)
                ds.Tables["TableKhoaHoc"].Clear();
            string sql = @"select MaKhoaHoc, TenKhoaHoc from DM_KHOAHOC
                            where MaDonVi = '" + _companyid + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.Fill(ds, "TableKhoaHoc");
        }

        private void UpdateData()
        {
            try
            {
                string sql = @"select * from BoHoc";
                SqlDataAdapter daupdate = new SqlDataAdapter(sql, conn);
                DataHelper.buildDataCommands(ref daupdate);
                daupdate.Update(ds, "TableBoHoc");
                LoadControl();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
